package com.jiyici.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiyici.R;
import com.jiyici.model.Item;

public class ItemView extends LinearLayout implements View.OnClickListener {

    public ItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflater();
    }

    public ItemView(Context context) {
        super(context);
        inflater();
    }

    public void setItem(Item item) {
        mItem = item;
        syncDataUI();
    }
    
    public Item getItem() {
        return mItem;
    }
    
    public void displayDivider(boolean display) {
        mDivider.setVisibility(display ? VISIBLE : INVISIBLE);
    }

    private void inflater() {
        mLayoutInflater = LayoutInflater.from(getContext());
        View view = mLayoutInflater.inflate(R.layout.view_item, this, true);
        mTextItemName = (TextView) view.findViewById(R.id.item_name);
        mDivider = view.findViewById(R.id.divider);
        View btnAddOne = view.findViewById(R.id.btn_add_one);
        btnAddOne.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_add_one) {
            DialogUtil.showAddOneDialog(getContext(), getItem());
        }
    }

    private void syncDataUI() {
        mTextItemName.setText(mItem.itemName);
    }

    private LayoutInflater mLayoutInflater;

    private TextView mTextItemName;
    
    private View mDivider;

    private Item mItem;

}
